package leetcode;

import list.LinkedListCreate;
import list.ListNode;

public class OddEvenList {

	public static void main(String[] args) {
		int[] array = {1, 4, 6, 8, 5, 7, 10, 19, 20, 16};
		
		ListNode head = LinkedListCreate.createList(array);
		LinkedListCreate.printList(head);
		OddEvenList oddEvenList = new OddEvenList();
		head = oddEvenList.oddEvenList(head);
		LinkedListCreate.printList(head);
	}
	public ListNode oddEvenList(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode newHead = new ListNode(head.val);
		ListNode temp = newHead;
		ListNode node = head;
		while (node != null && node.next != null) {
			if (node.next.next == null) {
				newHead.next = null;
				break;
			} else {
				newHead.next = new ListNode(node.next.next.val);
			}
			newHead = newHead.next;
			node = node.next.next;
		}
		LinkedListCreate.printList(head);
		node = head.next;
		newHead.next = new ListNode(node.val);
		newHead = newHead.next;
		
		while (node != null && node.next != null) {
			System.out.println("curNode: " + node.val);
			if (node.next.next == null) {
				newHead.next = null;
				break;
			} else {
				newHead.next = new ListNode(node.next.next.val);
			}
			newHead = newHead.next;
			node = node.next.next;
		}
		return temp;
	}
}
